﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WoWDeckbuilder
{
    public partial class ucCardViewer : UserControl
    {
        // classes
        private Database db = new Database();
        private CardViewer Viewer;

        // variables

        // lists
        private List<CheckedListBox> checklistboxen = new List<CheckedListBox>();

        // other
        private Image DefaultImage;

        // constructor
        public ucCardViewer()
        {
            InitializeComponent();
        }

        #region Forum control (fill) function
        /// <summary>
        /// Fills the usercontroll with all the info.
        /// </summary>
        private void FillForm()
        {
            FillCboSearch();
            FillClbAll();
            FillSets();
            FillCardListBox();
        }

        /// <summary>
        /// Fills the different cardsets.
        /// </summary>
        private void FillSets()
        {
            List<string> list = db.GetColum("Sets", "Name", null);
            foreach (string item in list)
            {
                lbSets.Items.Add(item);
            }
        }

        /// <summary>
        /// Fills the card listbox with all cards.
        /// </summary>
        private void FillCardListBox()
        {
            lbCards.Items.Clear();
            foreach (Card single in Viewer.Cards())
            {
                lbCards.Items.Add(single.toString());
            }
        }

        /// <summary>
        /// Fills the cbo with all possible search options.
        /// </summary>
        private void FillCboSearch()
        {
            List<string> list = db.GetColum(cboSearch.Tag.ToString(), "Option", null);
            foreach (string item in list)
            {
                cboSearch.Items.Add(item);
            }
        }

        /// <summary>
        /// Fills the all checklistboxen with there info.
        /// </summary>
        private void FillClbAll()
        {
            foreach (var listbox in checklistboxen)
            {
                string name = listbox.Tag.ToString();
                List<string> list = db.GetColum(name, "Name", null);
                foreach (string item in list)
                {
                    listbox.Items.Add(item);
                }
                int max = listbox.Items.Count;
                int count = 0;
                while (count < max)
                {
                    listbox.SetItemChecked(count, true);
                    count++;
                }
            }
        }
        #endregion

        #region Fill card info
        /// <summary>
        /// Gets the image from file (in the map "Images").
        /// If the imagefile can't be found use default image.
        /// </summary>
        /// <param name="file">The filename.</param>
        /// <returns>Image object</returns>
        private Image GetImage(string file)
        {
            Image img;
            try
            {
                img = Image.FromFile(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\Images\\" + file + ".jpg");
            }
            catch
            {
                img = DefaultImage;
            }
            return img;
        }
        /// <summary>
        /// Clears the card info on the usercontroll.
        /// </summary>
        private void clearCardInfo()
        {
            txtName.Text = "";
            txtClass.Text = "";
            txtCost.Text = "";
            txtFaction.Text = "";
            txtRarerty.Text = "";
            txtRace.Text = "";
            txtType.Text = "";
            rtbAbility.Text = "";
            rtbText.Text = "";
            txtTalent.Text = "";
            picCard.Image = null;
        }
        /// <summary>
        /// Fills the card info on the usercontroll.
        /// </summary>
        /// <param name="single">The card</param>
        private void fillCardInfo(Card single)
        {
            clearCardInfo();
            txtName.Text = single.Name;
            txtClass.Text = single.Class;
            txtCost.Text = single.Cost;
            txtFaction.Text = single.Faction;
            txtRarerty.Text = single.Rarety;
            txtType.Text = single.Type.ToString();
            rtbAbility.Text = single.Ability;
            rtbText.Text = single.Text;
            picCard.Image = GetImage(single.CardNumber);
            switch (single.Type)
            {
                case CardType.Ability:
                    Ability ab = (Ability)single;
                    txtTalent.Text = ab.Talent;
                    break;
                case CardType.Hero:
                    Hero hr = (Hero)single;
                    txtTalent.Text = hr.Talent;
                    txtRace.Text = hr.Race;
                    break;
                case CardType.Ally:
                    Ally ll = (Ally)single;
                    txtRace.Text = ll.Race;
                    break;
                case CardType.Equipment:
                    break;
                case CardType.Location:
                    break;
                case CardType.Quest:
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region Forum Events
        /// <summary>
        /// When a card is selected in lbCards, then the info will be displayed in the UserControll.
        /// </summary>
        private void lbCards_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] cardstring = lbCards.SelectedItem.ToString().Split(char.Parse(":"));
            Card one = new Card(null);
            foreach (Card single in Viewer.Cards())
            {
                if (single.CardNumber == cardstring[0])
                {
                    one = single;
                }
            }
            fillCardInfo(one);
        }

        /// <summary>
        /// When a set is selected the list of cards need to be adjusted compare to the sets that are selected.
        /// note: Underconstruction
        /// </summary>
        private void lbSets_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (var item in lbSets.SelectedItems)
            {
                string temp = db.GetValue("Sets", "SetID", "Name = '" + item.ToString() + "'");
                //ToDo: select cards from the selected set
            }
        }

        /// <summary>
        /// When the usercontroll loads:
        /// - use the default image in the picturebox
        /// - clear all checklistboxen and add the once that will be used
        /// - prepare the cardviewer class
        /// - fill the UserControll
        /// </summary>
        private void ucCardViewer_Load(object sender, EventArgs e)
        {
            DefaultImage = picCard.Image;
            checklistboxen.Clear();
            checklistboxen.Add(clbCardType);
            checklistboxen.Add(clbClass);
            checklistboxen.Add(clbFaction);
            checklistboxen.Add(clbOther);
            checklistboxen.Add(clbRace);
            checklistboxen.Add(clbRarity);
            checklistboxen.Add(clbReputation);
            checklistboxen.Add(clbSlot);
            Viewer = new CardViewer();
            FillForm();
        }
        #endregion

        /// <summary>
        /// Closes the database connection when the usercontroll closes.
        /// </summary>
        public void Close()
        {
            db.Close();
        }

        public void Reload()
        {
        }

        /// <summary>
        /// When an item in the checklistbox is check/uncheck edit the filter in the CardViewer class (Viewer)
        /// </summary>
        private void clb_SelectedIndexChanged(object sender, ItemCheckEventArgs e)
        {
            CheckedListBox clb = (CheckedListBox)sender;
            List<string> stay = new List<string>();
            var filter = clbCardType.CheckedItems;
            foreach (var items in clb.Items)
            {
                stay.Add(items.ToString());
            }
            Viewer.Filter(clb.Tag.ToString(), stay);
            FillCardListBox();
        }
    }
}
